﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Transfer;
using System.Threading;
using BranchOfSansar.Register.Reports.Forms;



namespace BranchOfSansar.Register.Trans
{
    public partial class frmTransBase : Form
    {
        public BranchOfSansar.Sansar.Enquiry.vw_Contracts Contract { get; set; }
        public delegate void ContractSelect();
        public event ContractSelect ContractSelected;
        public const int MinWidth = 562;
        public const int MaxWidth = 1094;
        public long JrNo = 0;

        public frmTransBase()
        {
            InitializeComponent();
        }


        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
        protected void FillData()
        {
            txtContractNo.Text = this.Contract.ContractNo.ToString();
            txtCustName.Text = Contract.CustName + ", " + Contract.CustRegister+", "+Contract.UserName;
            txtAddress.Text = string.Format("{0} байр {1} тоот", Contract.ApartNo1, Contract.DoorNo);
            txtBalance.Text = this.Contract.Balance.ToString("#,##0");
            cmbTransCategory.Enabled = true;
            using (BranchOfSansar.Sansar.Enquiry.EnquiryClient tc = new BranchOfSansar.Sansar.Enquiry.EnquiryClient(Globals.Token))
            {
                cmbTransCategory.Properties.Items.AddRange(tc.TransferCategorys());
                cmbTransCategory.SelectedIndex = 0;
            }
            if (ContractSelected != null)
                ContractSelected();
        }
        private void txtContractNo_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            BranchOfSansar.Register.Main.frmContractList frm = new BranchOfSansar.Register.Main.frmContractList(true);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Contract = frm.Contract;
                if (this.Contract.Status == 1)
                {
                    txtCustName.Text = "Гэрээ цуцлагдсан байна!";
                    return;
                }
                if (this.Contract.Status == 2 && this.GetType().Name != "frmTransRun")
                {
                    txtCustName.Text = "Гэрээ тvр хаагдсан байна!";
                    return;
                }
                FillData();
            }
        }

        protected void SetContractNo(long cno)
        {
            using (BranchOfSansar.Sansar.Enquiry.EnquiryClient tc = new Sansar.Enquiry.EnquiryClient(Globals.Token))
            {
                var res = tc.ContractDetail(cno);
                if (res == null)
                {
                    txtCustName.Text = "Гэрээний дугаар буруу!";
                    return;
                }
                if (res.Status == 1)
                {
                    txtCustName.Text = "Гэрээ цуцлагдсан байна!";
                    return;
                }
                if (res.Status == 2 && this.GetType().Name!="frmTransRun")
                {
                    txtCustName.Text = "Гэрээ тvр хаагдсан байна!" + this.GetType().Name;
                    return;
                }
                this.Contract = res;
                FillData();
            }
 
        }
        protected void SetContractNo(string uname)
        {
            using (BranchOfSansar.Sansar.Enquiry.EnquiryClient tc = new Sansar.Enquiry.EnquiryClient(Globals.Token))
            {
                var res = tc.ContractDetailByUser(uname);
                if (res == null)
                {
                    txtCustName.Text = "Хэрэглэгчийн дугаар буруу";
                    return;
                }
                this.Contract = res;
                FillData();
            }

        }
        private void txtContractNo_KeyUp(object sender, KeyEventArgs e)
        {
            long num;
            if (e.KeyCode == Keys.Enter)
            {
                if (long.TryParse(txtContractNo.Text, out num))
                    SetContractNo(num);
                else
                    SetContractNo(txtContractNo.Text);
            }
        }
        private void cmbTransCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            BranchOfSansar.Sansar.Enquiry.ComboItem item = (BranchOfSansar.Sansar.Enquiry.ComboItem)cmbTransCategory.SelectedItem;

            if (item.StrValue == "ACC")
                using (BranchOfSansar.Sansar.Enquiry.EnquiryClient tc = new BranchOfSansar.Sansar.Enquiry.EnquiryClient(Globals.Token))
                {
                    cmbBank.Enabled = true;
                    cmbBank.Properties.Items.AddRange(tc.Banks());
                    cmbBank.SelectedIndex = 0;
                }
            else
            {
                cmbBank.Enabled = false;
                cmbBank.Properties.Items.Clear();
            }
        }

        private void btnDetail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (btnDetail.Text == "Дэлгэрэнгүй >>")
            {
                long contno;
                if (long.TryParse(txtContractNo.Text, out contno))
                    using (TransferClient tc = new TransferClient(Globals.Token))
                    {
                        gridTrans.DataSource = tc.LastTransactions(contno);
                    }
                else
                    gridTrans.DataSource = null;

                this.Width = MaxWidth;
                btnDetail.Text = "<< Дэлгэрэнгүй";
            }
            else
            {

                this.Width = MinWidth;
                btnDetail.Text = "Дэлгэрэнгүй >>";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }

        public DialogResult PrintReceipt()
        {
            frmReceipt frm = new frmReceipt();
            using (BranchOfSansar.Sansar.Report.ReportClient rc = new Sansar.Report.ReportClient(Globals.Token))
            {
                BranchOfSansar.Sansar.Report.TransferReceipt res = rc.TransferInvoice(this.JrNo);
                
                return frm.ShowDialog();                
            }
        }

        private void txtContractNo_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void frmTransBase_Load(object sender, EventArgs e)
        {

        }
















    }
}
